<?php
	add_action('widgets_init', 'register_related_deals_wg');
	function register_related_deals_wg()
	{
		register_widget('related_deals');
	}

	class related_deals extends WP_Widget // widget class
	{
		function related_deals()// widget setting | class name must be same function name
		{
			$widget_ops = array('classname' => 'related-deals-w', 'description' => 'Display deals from related stores (right side bar)');
			$control_ops = array(
				'width' => 250,
				'height' => 350,
				'id_base' => 'related_deals');
			$this->WP_Widget('related_deals', 'DealX - Related Deals', $widget_ops, $control_ops);
		}

	function widget($args, $instance)// display widget
	{
            extract($args);
            $title = apply_filters('widget_title', $instance['title']);

            $number_stores = $instance['number_stores'];
            if(!$number_stores)
                $number_stores = 5;

            $number_deals = $instance['number_deals'];
            if(!$number_deals)
                $number_deals = 5;
            global $post;
            $deal_id = $post->ID;
            $store_id = get_post_meta($deal_id, 'store_id_metadata', true);
            // Get related deals id
            $relate_deals = cpx_get_related_deals($store_id, $deal_id, $number_stores, $number_deals);
            
            if(count($relate_deals) > 0):
            ?>
            <div class="right-sibar-wrap">
                <div class="right-sidebar-title">
                    <span><?php echo $title; ?></span>
                    <div class="clr"></div>                    
                </div>                
                <div class="all-deals-list row-fluid">
                <?php  
                    foreach ($relate_deals as $did)
                    {
                        cpx_get_deal_info($did, 1, 1);
                    }
                ?>
                </div>
            </div>
            <?php endif ?>
            <!-- GET SOME RANDOM ACTIVE DEALS -->
            <?php 
            $number_random_active_deals = 10 - count($relate_deals);
            if($number_random_active_deals > 0)
            {
            ?>
            <div class="right-sibar-wrap">
                <div class="right-sidebar-title">
                    <span><?php echo "Other active deals"; ?></span>
                    <div class="clr"></div>
                </div>
                <div class="all-deals-list row-fluid">
                <?php  
                    cpx_get_deals('', $number_random_active_deals, 0, 'active', 0, false, false, true, array($deal_id));
                ?>
                </div>
            </div>
            <?php 
            }
	}
	function update($new_instance, $old_instance)// update widget
	{
        $instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        $instance['number_stores'] = $new_instance['number_stores'];
        $instance['number_deals'] = $new_instance['number_deals'];
        return $instance;
	}

	function form($instance)
	{
	   ?>
         <div style="color: #333;">
    		<p>        
    			<label for="<?php echo $this->get_field_id('title');?>"><?php echo 'Title'; ?></label>
    			<input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php	echo $this->get_field_name('title');?>" value="<?php echo $instance['title'];?>" style="width:100%;" />
    		</p>
            <p>
    			<label for="<?php echo $this->get_field_id('number_stores');?>"><?php echo 'Get deals from';?>
    			<input type="text" id="<?php echo $this->get_field_id('number_stores');?>" name="<?php echo $this->get_field_name('number_stores');?>" value="<?php echo $instance['number_stores'];?>" style="width:15%;" />
                stores (default 5)</label>
    		</p>
            <p>
    			<label for="<?php echo $this->get_field_id('number_deals');?>"><?php echo 'Show';?>
    			<input type="text" id="<?php echo $this->get_field_id('number_deals');?>" name="<?php echo $this->get_field_name('number_deals');?>" value="<?php echo $instance['number_deals'];?>" style="width:15%;" />
                </label> deals per store (default 5)
    		</p>
        </div>
    <?php
	}
}
?>
